package cn.comtom.ewbs.signature.common.utils;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;

import org.springframework.util.Base64Utils;

/**
 *  base64 和 byte互相转换工具类
 */
public class Base64Util {
    /**
     * base64转成byte
     * @param base64Str
     * @return
     */
    public static byte[] base64ToByte(String base64Str){
    	return Base64Utils.decodeFromString(base64Str);
    }

    /**
     *  byte转成base64
     * @param b
     * @return
     */
    public static String byte2ToBase64(byte[] b){
    	return Base64Utils.encodeToString(b);
    }


    /**
     * 字符串转为byte数组类型(
     *
     * @param key
     * @return
     */
    public static byte[] strToByte(String key) {
        try {
            return key.getBytes(StandardCharsets.UTF_8.toString());
        } catch (UnsupportedEncodingException e) {
            return null;
        }
    }

    /**
     * byte数组转为字符串类型(
     *
     * @param bs
     * @return
     */
    public static String bytesToStr(byte[] bs) {
        try {
            return new String(bs, StandardCharsets.UTF_8.toString());
        } catch (UnsupportedEncodingException e) {
            return null;
        }
    }

    public static String strConvertToBase(String str) {
    	try {
			return Base64Utils.encodeToString(str.getBytes("UTF-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
    	return null;
    }
}
